package main.service.mail;

import main.service.mail.bean.EnterpriseMailBean;
import main.service.mail.bean.TokenBean;
import main.service.mail.db.Db;
import main.service.mail.db.JDBCLastTime;
import main.service.mail.db.JDBCMail;
import main.service.mail.http.HttpUtil;
import main.service.mail.util.EmailUtils;

import java.util.ArrayList;
import java.util.List;

// https://blog.csdn.net/Amy126/article/details/104914520
// https://exmail.qq.com/qy_mng_logic/doc#10029

/**
 * 添加时间
 * 账号
 * 开始时间 2021-08-09
 * 时间段为文件名
 * 结束时间 2021-08-09
 */
public class MailMain {
    public static final int http10 = 1;
    public static final int DB10 = 2;
    public static int type = http10;

    public static void main(String[] args) {
        getMail10();
    }

    public static List<EnterpriseMailBean> getMail10() {
        if (type == DB10)
            Db.main();
        input();
        if (type == DB10) {
            updateDatabase();
            getTop10Db();
        }
        try {
            return EmailUtils.readEmailPop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    private static List<EnterpriseMailBean> getTop10() {
        List<EnterpriseMailBean> list = JDBCMail.find(1);
        list.forEach(i -> {
            System.out.println(i + "---" + i.sendDate);
        });
        return list;
    }


    private static void updateDatabase() {
//        otherInfo();

//        mailLog();

        List<EnterpriseMailBean> list = new ArrayList<>();
        try {
            list = EmailUtils.readEmailPop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        list.sort((o1, o2) -> o2.sendDate.compareTo(o1.sendDate));
        for (int i = 0; i < list.size(); i++) {
            EnterpriseMailBean bean = list.get(i);
            JDBCMail.insert(bean.subject, bean.sender, bean.receiveAddress, bean.sendDate, bean.priority);
            System.out.println(i + "---" + list.get(i).sendDate);
        }
    }

    private static void otherInfo() {
        // 获取邮箱的 token 后，获取邮件未读
        TokenBean tokenBean = HttpUtil.getTokenBean(HttpUtil.MAIL);
        HttpUtil.getMailCountBean(tokenBean.access_token);

        // 获取登录的 token 后，单点登录
        TokenBean tokenBeanLogin = HttpUtil.getTokenBean(HttpUtil.LOGIN);
        HttpUtil.getLoginUrl(tokenBeanLogin.access_token);
    }

    private static void mailLog() {
        TokenBean tokenBeanLog = HttpUtil.getTokenBean(HttpUtil.LOG);
        HttpUtil.getLogOperation(tokenBeanLog.access_token);
        HttpUtil.getLoginLog(tokenBeanLog.access_token);
    }

    private static List<EnterpriseMailBean> getTop10Db() {
        List<EnterpriseMailBean> list = JDBCMail.find(1);
        list.forEach(i -> {
            System.out.println(i + "---" + i.sendDate);
        });
        return list;
    }

    private static void input() {
       /*
        System.out.print("输入开始时间（格式：2021-11-05）："); // 2021-11-05 代表 2021-11-05 00：00
        String startTime = s.nextLine();
        System.out.print("输入结束时间（格式：2021-12-06）："); // 2021-11-05 代表 2021-11-06 00：00
        EmailUtils.SAVE_DIR = "E:\\" + EmailUtils.ACCOUNT;*/
        EmailUtils.ACCOUNT = "qintianshu@jspdi.com.cn";
        EmailUtils.PASSWORD = "Qts970516";
        if (type == DB10)
            EmailUtils.START_TIME = JDBCLastTime.find(EmailUtils.ACCOUNT).getLastTime();
    }
}


